<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class images_model extends CI_Model {
	
    public $config = array(
        'img'=>array(
            'upload_path' => './uploads/images/data',
            'allowed_types' => 'gif|jpg|png|jpeg',
            'max_size' => 1000,
            'max_width' => 1912,
            'max_height' => 1106,
            'encrypt_name' => TRUE
        ),
        'thumb'=>array(
            'source_image' => '', //get original image
            'new_image' => './uploads/images/data/thumbs', //save as new image //need to create thumbs first
            'maintain_ratio' => true,
            'width' => 280,
            'height' => 200,
        ), 
        'gallery'=>array(
            'source_image' => '', //get original image
            'new_image' => './uploads/images/data/gallery', //save as new image //need to create thumbs first
            'maintain_ratio' => false,
            'width' => 700,
            'height' => 500,
            'image_library'=>'gd2',
            'y_axis' => 0,
            'x_axis' => 0,
        ),
        'slide'=>array(
            'source_image' => '', //get original image
            'new_image' => './uploads/images/data/slide', //save as new image //need to create thumbs first
            'maintain_ratio' => false,
            'width' => 960,
            'height' => 450,
            'image_library'=>'gd2',
            'y_axis' => 0,
            'x_axis' => 0,
        ),
        'slideThumb'=>array(
            'source_image' => '', //get original image
            'new_image' => './uploads/images/data/slide/thumbs', //save as new image //need to create thumbs first
            'maintain_ratio' => true,
            'width' => 220,
            'height' => 103,
        ),
    );
     
   	public function __construct()
    {
        parent::__construct();// a must for constructor
        $this->load->model('tdatabase_model', 'tdatabase');
        $this->load->library('image_lib'); //load library
    }
    
 	public function delete($imgID)
    {
        $db_images = $this->tdatabase->get_entry_where('id', $imgID, 'img');
        if (is_array($db_images)) {
            foreach ($db_images as $img_item) {
                $img = $img_item->image;
                $thumb = $img_item->thumbnail;
                if ($img != "" && $thumb != "") {
                    $this->tdatabase->remove_entry('id', $imgID, 'img');
                    unlink($this->config['img']['upload_path'] . "/" . $img);
                    unlink($this->config['thumb']['new_image'] . "/" . $thumb);
                    unlink($this->config['gallery']['new_image'] . "/" . $thumb);
                }
            }
        }
    }
         
 	public function upload($collID)
    {
   	    $this->load->library('upload', $this->config['img']);
		if ( ! $this->upload->upload("files".$collID)) {
            $data['error'] = $this->upload->display_errors();
            echo join('<br>', $data);
		} else {
            $upload_data = $this->upload->data();
                   
            #create gallery image.
            $this->image_lib->clear();
			$this->config['gallery']['source_image'] = $upload_data['full_path'];
            if ($upload_data['image_height'] / 500 > $upload_data['image_width'] / 700) {
                $this->config['gallery']['height'] = 500;
                $this->config['gallery']['width'] = 700;
            } else {
                $this->config['gallery']['height'] = 500;
                $this->config['gallery']['width'] = 7000;
            }
            $this->config['gallery']['maintain_ratio'] = TRUE;
            $this->image_lib->initialize($this->config['gallery']); 
            $this->image_lib->resize(); //do whatever specified in config     
            
            $this->image_lib->clear();
            $this->config['gallery']['source_image'] = $upload_data['file_path'] . "/gallery/" . $upload_data['file_name'];
            $this->config['gallery']['height'] = 500;
            $this->config['gallery']['width'] = 700;
            $this->config['gallery']['maintain_ratio'] = FALSE;
            $this->config['gallery']['x_axis'] = (($upload_data['image_width'] / ($upload_data['image_height'] / 500)) / 2) - 350;
            $this->image_lib->initialize($this->config['gallery']); 
            $this->image_lib->crop(); #do whatever specified in config
            
            # create thumbnail
            $this->image_lib->clear();
			$this->config['thumb']['source_image'] = $upload_data['file_path'] . "/gallery/" . $upload_data['file_name'];
            $this->image_lib->initialize($this->config['thumb']); 
            $this->image_lib->resize(); //do whatever specified in config
            
            $data['title'] = '';
            $data['thumbnail'] = $upload_data['file_name'];
            $data['image'] = $upload_data['file_name'];
            $data['description'] = '';
            $data['collID'] = $collID;
            $this->tdatabase->insert_entry($data, 'img');
		}
    }
    
    public function get($collID)
    {
        $images = $this->tdatabase->get_entry_where('collID', $collID, 'img');
        # to get the latest added at the first
        return array_reverse($images);
    }
      
    public function getTitle($imgID)
    {
        $data = $this->tdatabase->get_entry_where('id', $imgID, 'img');
        return $data[0]->title;
    }
        
    public function getDescription($imgID)
    {
        $data = $this->tdatabase->get_entry_where('id', $imgID, 'img');
        return $data[0]->description;
    }
      
    public function getHtmlBox($collID)
    {
        $images = $this->get($collID);
        $imagesHtml = '';
        if (is_array($images)) {
            foreach ($images as $img) {
                $data['box'] = $img;
                $imagesHtml .= $this->smarty->fetch('admin/gallery/imgBox.tpl', $data);
            }
        }
        return $imagesHtml;
    }
    
    public function getLatestImagesHTMLBox($count = 8) {
        $imgDB = array_slice(array_reverse($this->tdatabase->get_entry('img')), 0, $count);
        $imagesHtml = '';
        foreach ($imgDB as $img) {
            $data['box']['img'] = site_url("uploads/images/data/thumbs/" . $img->thumbnail);
            $data['box']['url'] = site_url("gallery/collection/" . $img->collID);
            $imagesHtml .= $this->smarty->fetch('latestProjects/smallImgBox.tpl', $data);
        }
        return $imagesHtml;
    }
    
    public function getSlideImages()
    {
        $images = $this->tdatabase->get_entry('slide');
        $imagesHtml = '';
        if (is_array($images)) {
            foreach ($images as $img) {
                $data['box'] = $img;
                $imagesHtml .= $this->smarty->fetch('admin/slide/imgBox.tpl', $data);
            }
        }
        return $imagesHtml;
    }
    
   	public function uploadSlide($collID)
    {
   	    $this->load->library('upload', $this->config['img']);
		if ( ! $this->upload->upload("files".$collID)) {
            $data['error'] = $this->upload->display_errors();
            echo join('<br>', $data);
		} else {
            $upload_data = $this->upload->data();
                   
            #create slide image.
            $this->image_lib->clear();
			$this->config['slide']['source_image'] = $upload_data['full_path'];
            if ($upload_data['image_height'] / 450 > $upload_data['image_width'] / 960) {
                $this->config['slide']['height'] = 4500;
                $this->config['slide']['width'] = 960;
            } else {
                $this->config['slide']['height'] = 450;
                $this->config['slide']['width'] = 9600;
            }
            $this->config['slide']['maintain_ratio'] = TRUE;
            $this->image_lib->initialize($this->config['slide']); 
            $this->image_lib->resize(); //do whatever specified in config     
            
            $this->image_lib->clear();
            $this->config['slide']['source_image'] = $upload_data['file_path'] . "/slide/" . $upload_data['file_name'];
            $this->config['slide']['height'] = 450;
            $this->config['slide']['width'] = 960;
            $this->config['slide']['maintain_ratio'] = FALSE;
            $this->image_lib->initialize($this->config['slide']); 
            $this->image_lib->crop(); #do whatever specified in config
            
            # create thumbnail
            $this->image_lib->clear();
			$this->config['slideThumb']['source_image'] = $upload_data['file_path'] . "/slide/" . $upload_data['file_name'];
            $this->image_lib->initialize($this->config['slideThumb']); 
            $this->image_lib->resize(); //do whatever specified in config
            
            $data['title'] = '';
            $data['thumbnail'] = $upload_data['file_name'];
            $data['image'] = $upload_data['file_name'];
            $data['description'] = '';
            $data['collID'] = $collID;
            $this->tdatabase->insert_entry($data, 'slide');
		}
    }
    
    public function update($imgID, $title, $desc) {
        $data['id'] = $imgID;
        $data['title'] = $title;
        $data['description'] = $desc;
        $this->tdatabase->update_entry($data, 'img');
    }
}
